import urls from "./urls.js";
import store from "../store/index.js";

const showToast = (title) => {
	uni.showToast({
		title: title,
		icon: 'none'
	})
}

/**请求接口
 * @method http
 * @param {String} url 接口地址
 * @param {Object} data 接口参数
 * @param {Object} option 接口配置信息，可选
 * @return {Object} Promise
 */
const http = (url, data = {}, option = {}) => {
	let hideLoading = option.hideLoading || false // 是否显示 loading
	let hideMsg = option.hideMsg || false // 是否隐藏错误提示
	let token = store.state.token;
	if (!hideLoading) {
		uni.showLoading({
			title: '加载中...',
			mask: true
		})
	}
	return new Promise((resolve, reject) => {
		uni.request({
			url: url,
			method: option.method || 'GET', // 默认 get 请求
			header: {
				'Authorization': token
			},
			data: data,
			success: res => {
				if (!hideLoading) uni.hideLoading()
				if (res.statusCode === 200) {
					let result = res.data
					if (result.code === 0) {
						resolve(result)
						return;
					}
					if (result.code === 2307) {
						// 长时间未操作，token失效
						uni.navigateTo({
							url: "../login/login",
							success: () => {
								uni.showToast({
									title: result.message,
									icon: "none",
								});
							},
						});
					}
					reject(result)
					if (!hideMsg) showToast(result.message);
				} else { // 返回值非 200，强制显示提示信息
					showToast('[' + res.statusCode + '] 服务器忙，请稍后再试')
					reject('[' + res.statusCode + '] 服务器忙，请稍后再试')
				}
			},
			fail: (err) => { // 接口调用失败的回调函数
				if (!hideLoading) uni.hideLoading()
				console.log(err);
				if (err.errMsg != 'request:fail abort') {
					showToast('连接超时，请检查您的网络。')
					reject('连接超时，请检查您的网络。')
				}
			}
		})
	})
}

const fetchImagesApi = (param) => {
	return http(urls.getImagesUrl, param, {
		hideLoading: true, // 默认 false
		hideMsg: false, // 默认 false
		method: 'GET' // 默认 get
	});
}

const fetchRecommendImagesApi = (param) => {
	return http(urls.getRecommendImagesUrl, param, {
		hideLoading: true, // 默认 false
		hideMsg: false, // 默认 false
		method: 'GET' // 默认 get
	});
}

const loginApi = (data) => {
	return http(urls.loginUrl, data, {
		hideLoading: false,
		hideMsg: false,
		method: 'POST'
	});
}

const fetchImageInfoByIdApi = (id) => {
	return http(urls.addImageUrl + '/' + id);
}

const addCommentApi = (data) => {
	return http(urls.addCommentUrl, data, {
		hideLoading: true,
		hideMsg: false,
		method: 'POST'
	});
}

const fetchCommentsApi = (data) => {
	return http(urls.getCommentsUrl, data, {
		hideLoading: true,
		hideMsg: false,
		method: 'GET'
	});
}

const doImageLikeApi = (data) => {
	return http(urls.likeImageUrl, data, {
		hideLoading: true,
		hideMsg: false,
		method: 'POST'
	});
}

const cancelImageLikeApi = (data) => {
	return http(urls.cancelLikeImageUrl, data, {
		hideLoading: true,
		hideMsg: false,
		method: 'POST'
	});
}

const doImageCollectApi = (data) => {
	return http(urls.collectImageUrl, data, {
		hideLoading: true,
		hideMsg: false,
		method: 'POST'
	});
}

const cancelImageCollectApi = (data) => {
	return http(urls.cancelCollectImageUrl, data, {
		hideLoading: true,
		hideMsg: false,
		method: 'POST'
	});
}

const addImageApi = (data) => {
	return http(urls.addImageUrl, data, {
		hideLoading: true,
		hideMsg: false,
		method: 'POST'
	});
}

const logoutApi = (data) => {
	return http(urls.logoutUrl, data, {
		hideLoading: true,
		hideMsg: false,
		method: 'GET'
	});
}

const delImageApi = (id) => {
	return http(urls.addImageUrl + "/" + id, {}, {
		hideLoading: true,
		hideMsg: false,
		method: 'DELETE'
	});
}

const fetchImageLikes = (data) => {
	return http(urls.getLikeImagesUrl, data, {
		hideLoading: true,
		hideMsg: false,
		method: 'GET'
	});
}

const fetchImageCollections = (data) => {
	return http(urls.getCollectedImagesUrl, data, {
		hideLoading: true,
		hideMsg: false,
		method: 'GET'
	});
}

const fetchUserInfoApi = (data) => {
	return http(urls.getUserInfoUrl, data, {
		hideLoading: true,
		hideMsg: false,
		method: 'GET'
	});
}

const fetchNoticesApi = (data) => {
	return http(urls.getNoticesUrl, data, {
		hideLoading: true,
		hideMsg: false,
		method: 'GET'
	});
}

const delNoticeByIdApi = (id) => {
	return http(urls.noticeUrl + "/" + id, {}, {
		hideLoading: true,
		hideMsg: false,
		method: 'DELETE'
	});
}

const updateNoticeStatusApi = (data) => {
	return http(urls.noticeUrl, data, {
		hideLoading: true,
		hideMsg: false,
		method: 'PUT'
	});
}

const getPublicKeyForRegisterApi = () => {
	return new Promise((resolve, reject) => {
		uni.request({
			url: urls.getPubKeyUrl,
			method: 'GET',
			success: res => {
				if (res.statusCode === 200) {
					let result = res.data
					if (result.code === 0) {
						resolve(res)
						return;
					}
					reject(result)
					showToast(result.message);
				} else {
					showToast('[' + res.statusCode + '] 服务器忙，请稍后再试')
					reject('[' + res.statusCode + '] 服务器忙，请稍后再试')
				}
			},
			fail: (err) => {
				console.log(err);
				if (err.errMsg != 'request:fail abort') {
					showToast('连接超时，请检查您的网络。')
				}
			}
		})
	})
}

const userRegisterApi = (data) => {
	return http(urls.userRegisterUrl, data, {
		hideLoading: true,
		hideMsg: false,
		method: 'POST'
	});
}

const fetchImageDailyRankApi = () => {
	return http(urls.imageDailyRankUrl, {}, {
		hideLoading: true,
		hideMsg: false,
		method: 'GET'
	});
}

const getKeywordsByPrefixApi = (prefix) => {
	return http(urls.getKeywordsByPrefixUrl + "/" + prefix, {}, {
		hideLoading: true,
		hideMsg: true,
		method: 'GET'
	});
}

const fetchImagesByKeywordsApi = (data) => {
	return http(urls.queryByKeywordsUrl, data, {
		hideLoading: false,
		hideMsg: false,
		method: 'GET'
	})
}

const updateUserInfoByIdApi = (data) => {
	return http(urls.updateUserInfoUrl, data, {
		hideLoading: false,
		hideMsg: false,
		method: 'POST'
	});
}

const getSimpleUserInfoByIdApi = (userId) => {
	return http(urls.simpleUserUrl + "/" + userId, {}, {
		hideLoading: true,
		hideMsg: false,
		method: 'GET'
	});
}

const getFollowedUsersImages = (params) => {
	return http(urls.getFollowedUsersImagesUrl, params, {
		hideLoading: false,
		hideMsg: false,
		method: 'GET'
	});
}

const cancelFollowUser = (data) => {
	return http(urls.cancelFollowUserUrl, data, {
		hideLoading: true,
		hideMsg: false,
		method: 'POST'
	});
}

const doFollowUser = (data) => {
	return http(urls.doFollowUserUrl, data, {
		hideLoading: true,
		hideMsg: false,
		method: 'POST'
	});
}

const fetchOssTokenApi = () => {
	return http(urls.getOssTokenUrl, {}, {
		hideLoading: false,
		hideMsg: false,
		method: 'GET'
	})
}

const fetchImagesByLabelNameApi = (params) => {
	return http(urls.getImagesByLabelNameUrl, params, {
		hideLoading: false,
		hideMsg: false,
		method: 'GET'
	})
}


export {
	fetchImagesApi,
	loginApi,
	fetchImageInfoByIdApi,
	addCommentApi,
	fetchCommentsApi,
	doImageLikeApi,
	cancelImageLikeApi,
	doImageCollectApi,
	cancelImageCollectApi,
	addImageApi,
	logoutApi,
	delImageApi,
	fetchImageLikes,
	fetchImageCollections,
	fetchUserInfoApi,
	fetchNoticesApi,
	delNoticeByIdApi,
	updateNoticeStatusApi,
	getPublicKeyForRegisterApi,
	userRegisterApi,
	fetchImageDailyRankApi,
	getKeywordsByPrefixApi,
	updateUserInfoByIdApi,
	getSimpleUserInfoByIdApi,
	getFollowedUsersImages,
	cancelFollowUser,
	doFollowUser,
	fetchOssTokenApi,
	fetchImagesByKeywordsApi,
	fetchImagesByLabelNameApi,
	fetchRecommendImagesApi
};
